//
//  HCMineViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCMineViewController.h"

#import "HCMineHeaderView.h"
#import "HCMineContentView.h"
#import "HCLogViewController.h"

#import "HCMineModel.h"
#import "HCLogViewController.h"

#import "XFWkwebView.h"
#import <AFNetworking.h>

#import "HCMineHeadView.h"

@interface HCMineViewController () <HCMineHeaderViewDelegate,HCMineContentViewDelegate,UIScrollViewDelegate> {
    
    HCMineHeaderView *mine_headerView;
    HCMineContentView *mine_contentView;
    HCMineModel *model;
    UIScrollView *mineScrollView;
    CGRect originalFrame;
    BOOL isVIP_;
}

@property (nonatomic, strong) HCMineHeadView *userHeadView;


@end

@implementation HCMineViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.dataRequest.delegate = self;
    [self.dataRequest postHC_MineMessages:HCGETUSER(@"userid")];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置title
//    self.textTitle = @"我的";
    self.headerView.hidden = YES;
    self.centerView.frame = CGRectMake(0, 0, Screen_width, Screen_height-TabBarHeight);
    mineScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, Screen_height-TabBarHeight)];
    mineScrollView.scrollEnabled = YES;
    mineScrollView.showsVerticalScrollIndicator = NO;
    mineScrollView.showsHorizontalScrollIndicator = NO;
    mineScrollView.contentSize = CGSizeMake(Screen_width, Screen_height-20);
    mineScrollView.contentOffset = CGPointMake(0, 0);
    mineScrollView.delegate = self;
    isVIP_ = NO;
    [self.centerView addSubview:mineScrollView];
    
    
    //设置背景颜色
    self.centerView.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    
    //设置右上角按钮
    [self setRightButtonImage:@"happy_mine_rightitemimage"];
    
    //初始化顶部
    [self initHeaderView1];
    
    //初始化内容
    [self initContentView];
}

#pragma mark 初始化头部视图
-(void)initHeaderView1 {
    
    self.userHeadView = [[HCMineHeadView alloc] initWithFrame:CGRectMake(0, 0, YHGetScreenWidth, YBKAdaptiveH(171))];
    self.userHeadView.backgroundColor = [UIColor whiteColor];
    originalFrame = self.userHeadView.frame;
    [mineScrollView addSubview:self.userHeadView];
    
    __weak typeof (self)weakSelf = self;
    //优惠
    [weakSelf.userHeadView setClickCouponsButtonBlock:^{
        
    }];
    
    //积分
    [weakSelf.userHeadView setClickIntegralButtonBlock:^{
        
    }];
    
    //头像
    [weakSelf.userHeadView setUserHeadClickBlock:^{
        
        if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
            HCLogViewController *log = [[HCLogViewController alloc] init];
            
            UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
            na.navigationBarHidden = YES;
            [self.navigationController presentViewController:na animated:YES completion:nil];
        }else{
            [self pushChildrenViewController:@"HCMindHeadViewController" parameterObject:nil dataObject:nil];
        }
    }];
}

#pragma mark -- UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGFloat yoffset = scrollView.contentOffset.y;
    if (yoffset < 0) {
        
        CGRect frame = self.userHeadView.frame;
        frame.origin.y = yoffset;
        frame.size.height = fabs(-yoffset) + originalFrame.size.height;
        CGFloat rate = (-yoffset)/originalFrame.size.height;
        frame.size.width = Screen_width*(1+rate);
        frame.origin.x = -rate*originalFrame.size.width/2.0;
        self.userHeadView.backImageView.frame = frame;
    }
}

#pragma mark 初始化具体内容视图
-(void)initContentView {
    
    mine_contentView = [[HCMineContentView alloc] initWithFrame:CGRectMake(0, YBKAdaptiveH(181), YHGetScreenWidth, YBKAdaptiveH((self.centerView.frame.size.height-YBKAdaptiveH(141))) )];
    mine_contentView.backgroundColor = [UIColor whiteColor];
    mine_contentView.delegate = self;
    [mineScrollView addSubview:mine_contentView];
}

#pragma mark 顶部点击代理
-(void)hcMineHeaderViewUserInfoClicked:(HCMineHeaderView *)view {
    
    if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                HCLogViewController *log = [[HCLogViewController alloc] init];
        
                UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                na.navigationBarHidden = YES;
                [self.navigationController presentViewController:na animated:YES completion:nil];
    }else{
        [self pushChildrenViewController:@"HCMindHeadViewController" parameterObject:nil dataObject:nil];
    }
}

#pragma mark 余额，积分，优惠
-(void)hcMineHeaderViewClickedStyle:(HCMineHeaderViewClickStyle)style {
    switch (style) {
        case HCMineHeaderViewMoneyClicked:
            //余额
            if (!mine_headerView.imageView_VIP.hidden) {
                if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                    HCLogViewController *log = [[HCLogViewController alloc] init];
                    
                    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                    na.navigationBarHidden = YES;
                    [self.navigationController presentViewController:na animated:YES completion:nil];
                }else{
                    [self pushChildrenViewController:@"HCVIPViewController" parameterObject:nil dataObject:nil];
                }
            }
            break;
        case HCMineHeaderViewScoreClicked:
            //积分
            NSLog(@"进入积分");
            break;
        case HCMineHeaderViewPreferentialClicked:
            //优惠
            NSLog(@"进入优惠");
            break;
        default:
            break;
    }
}

#pragma mark 内容部分点击代理
-(void)hcMineContentViewCellClickedIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                HCLogViewController *log = [[HCLogViewController alloc] init];
                
                UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                na.navigationBarHidden = YES;
                [self.navigationController presentViewController:na animated:YES completion:nil];
            }else{
                [self pushChildrenViewController:@"HCMineAppViewController" parameterObject:nil dataObject:nil];
            }
        }
        else if (indexPath.row == 1) {
        
            //点击我的会员
            if (isVIP_) {
                
                [self pushChildrenViewController:@"HCVIPViewController" parameterObject:nil dataObject:nil];
            }
            else {
            
                [XHToast showBottomWithText:@"请升级为会员"];
            }
        }
        else if(indexPath.row == 2){
            
            if (!mine_headerView.imageView_VIP.hidden) {
                if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                    HCLogViewController *log = [[HCLogViewController alloc] init];
                    
                    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                    na.navigationBarHidden = YES;
                    [self.navigationController presentViewController:na animated:YES completion:nil];
                }else{
                    [self pushChildrenViewController:@"HCGrowthAlbumViewController" parameterObject:nil dataObject:nil];
                }
            }else{
                if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                    HCLogViewController *log = [[HCLogViewController alloc] init];
                    
                    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                    na.navigationBarHidden = YES;
                    [self.navigationController presentViewController:na animated:YES completion:nil];
                }else{
                    [self pushChildrenViewController:@"HCGrowthAlbumViewController" parameterObject:nil dataObject:nil];
                }
            }
            
        }else if (indexPath.row == 3){
            NSLog(@"我的兑换");
            [self pushChildrenViewController:@"HCMineExchangeViewController" parameterObject:nil dataObject:nil];
        }else if (indexPath.row == 4){
            //我的收藏
            [self pushChildrenViewController:@"HCMineCollectViewController" parameterObject:nil dataObject:nil];
        }
        else {
        
            //地址管理
            [self pushChildrenViewController:@"HCAddressManagerController" parameterObject:nil dataObject:nil];
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {

            [self pushChildrenViewController:@"HCScoreStoreViewController" parameterObject:nil dataObject:nil];
            
        }else if (indexPath.row == 1) {
            //积分商城
        }else{
            
        }
    }else{
        if (indexPath.row == 0) {
            [self pushChildrenViewController:@"HCCtnterViewController" parameterObject:nil dataObject:nil];
        }else if (indexPath.row == 1) {
            
            NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id1216002651"];
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    }
}

#pragma mark 网络请求返回
-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    NSLog(@"个人信息界面返回数据:%@",dictionary);
    
    if (dictionary.count != 0 && [dictionary[@"index"][@"stat"] integerValue] == 200) {
        
        self.userHeadView.userInfo = dictionary[@"index"];
        NSString *vipcardNum = [NSString stringWithFormat:@"%@",[[dictionary objectForKey:@"index"] objectForKey:@"vipcard"]];
        if ([PublicFunction isNull:vipcardNum] || vipcardNum == NULL || vipcardNum.length == 0 || vipcardNum == nil) {
            
            [mine_contentView setLabel:@"非会员" isVip:NO];
            isVIP_ = NO;
        }
        else {
            [mine_contentView setLabel:vipcardNum isVip:YES];
            isVIP_ = YES;
        }
    }
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"个人信息网络请求失败");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
